<?php

namespace app\admin\controller;

use app\admin\common\Common;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\facade\Validate;
use think\Request;

class Bbs extends LoginThere
{
    public function plate(){
        try {
            $listPlate = Db::name('plate')
                ->alias('p')
                ->join('app a', 'a.appid = p.appid')
                ->field('p.*,a.appname')
                ->paginate(10);
        } catch (DbException $e) {
             return Common::result(400,$e->getMessage());
        }
        $page = $listPlate->render();
        return $this->fetch('/bbs/index', ['list' => $listPlate, 'page' => $page]);
    }


    public function addplate(Request $request){
        $data = $request->param();
        $validate = Validate::make([
            'platename'  => 'require',
            'plateicon' => 'require',
            'appid' => 'require|number',
        ]);
        if (!$validate->check($data)) {
            return Common::result(400,$validate->getError());
        }
        try {
            $app = Db::name('app')->where('appid', $data['appid'])->find();
        } catch (DataNotFoundException $e) {
             return Common::result(400,$e->getMessage());
        } catch (ModelNotFoundException $e) {
             return Common::result(400,$e->getMessage());
        } catch (DbException $e) {
             return Common::result(400,$e->getMessage());
        }
        if ($app == null){
            return Common::result(400,'app不存在');
        }
        $adddata = [
            'platename' => $data['platename'],
            'plateicon' => $data['plateicon'],
            'appid' => $data['appid'],
            'creattime' => date("Y-m-d H:i:s",time()),
        ];
        $db = Db::name('plate')->insert($adddata);
        if ($db > 0){
            return Common::result(200,"添加成功");
        }else{
            return Common::result(400,"添加失败");
        }
    }

    public function delplate(Request $request){
        $data = $request->post();
        try {
            $user = Db::name('plate')->delete($data['id']);
        } catch (PDOException $e) {
             return Common::result(400,$e->getMessage());
        } catch (Exception $e) {
             return Common::result(400,$e->getMessage());
        }
        if ($user>0){
            return Common::result(200,"删除成功");
        }else{
            return Common::result(400,'删除失败');
        }
    }

    public function platepost(){
        try {
            $listPost = Db::name('post')
                ->alias('p')
                ->join('app a', 'a.appid = p.appid')
                ->join('plate q', 'q.appid = p.appid')
                ->field('p.*,a.appname,q.platename,(SELECT COUNT(*) FROM mr_comment as c WHERE c.postid  = p.id) as commentnum')
                ->paginate(10);
        } catch (DbException $e) {
             return Common::result(400,$e->getMessage());
        }
        $page = $listPost->render();
        return $this->fetch('/bbs/platepost', ['list' => $listPost, 'page' => $page]);
    }

    public function delplatepost(Request $request){
        $data = $request->post();
        try {
            $user = Db::name('post')->delete($data['id']);
        } catch (PDOException $e) {
             return Common::result(400,$e->getMessage());
        } catch (Exception $e) {
             return Common::result(400,$e->getMessage());
        }
        if ($user>0){
            return Common::result(200,"删除成功");
        }else{
            return Common::result(400,'删除失败');
        }
    }

    public function querypost($id){
        try {
            $listPost = Db::name('post')->where('p.id', $id)
                ->alias('p')
                ->join('app a', 'a.appid = p.appid')
                ->join('plate q', 'q.appid = p.appid')
                ->field('p.*,a.appname,q.platename')
                ->find();
            return $this->fetch('bbs/querypost')->assign('post',$listPost);
        } catch (DataNotFoundException $e) {
             return Common::result(400,$e->getMessage());
        } catch (ModelNotFoundException $e) {
             return Common::result(400,$e->getMessage());
        } catch (DbException $e) {
             return Common::result(400,$e->getMessage());
        }
    }
    public function updatepost(Request $request){
        $data = $request->post();
        $validate = Validate::make([
            'id' => 'require|number',
            'postname'  => 'require',
            'postcontent' => 'require',
            'lock' => 'require|number',
            'top' => 'require|number',
        ]);
        if (!$validate->check($data)) {
            return Common::result(400,$validate->getError());
        }
        $update = [
            'postname' => $data['postname'],
            'postcontent' => htmlspecialchars_decode($data['postcontent']),
            'lock' => $data['lock'],
            'top' => $data['top'],
        ];
        $db = Db::name('post')->where('id',$data['id'])->update($update);
        if ($db > 0){
            return Common::result(200,"修改成功");
        }else{
            return Common::result(400,"修改失败");
        }
    }

    public function queryplate($id){
        try {
            $listPlate = Db::name('plate')->where('p.id', $id)
                ->alias('p')
                ->join('app a', 'a.appid = p.appid')
                ->field('p.*,a.appname')
                ->find();
            return $this->fetch('bbs/queryplate')->assign('plate',$listPlate);
        } catch (DataNotFoundException $e) {
            return Common::result(400,$e->getMessage());
        } catch (ModelNotFoundException $e) {
            return Common::result(400,$e->getMessage());
        } catch (DbException $e) {
            return Common::result(400,$e->getMessage());
        }
    }

    public function updateplate(Request $request){
        $data = $request->post();
        $validate = Validate::make([
            'id' => 'require|number',
            'platename'  => 'require',
            'plateicon' => 'require',
        ]);
        if (!$validate->check($data)) {
            return Common::result(400,$validate->getError());
        }
        $update = [
            'platename' => $data['platename'],
            'plateicon' => $data['plateicon'],
        ];
        $db = Db::name('plate')->where('id',$data['id'])->update($update);
        if ($db > 0){
            return Common::result(200,"修改成功");
        }else{
            return Common::result(400,"修改失败");
        }
    }
}